package com.hk.reply;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hk.action.Action;
import com.hk.action.ActionForward;
import com.hk.dtos.BBSDto;
import com.hk.dtos.PhotoDto;
import com.hk.dtos.ReplyDto;
import com.hk.file.FileManager;
import com.hk.service.BBSService;
import com.hk.service.PhotoService;
import com.hk.service.ReplyService;
import com.jungbo.file.dto.FileDto;

public class ReplyInsAction implements Action{
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward forword=new ActionForward();
		BBSService bmanager = BBSService.getInstance();
		PhotoService pmanager = PhotoService.getInstance();
		ReplyService manager=ReplyService.getInstance();
		String id = request.getParameter("id");
		String tbid = request.getParameter("bid");
		String content = request.getParameter("content");
		String tparent = request.getParameter("seq");
		String bbsname = request.getParameter("bbsname");
		
		int parent = Integer.parseInt(tparent);
		
		int bid = Integer.parseInt(tbid);
		
		
		ReplyDto rd = new ReplyDto();
		
		rd.setId(id);
		rd.setContent(content);
		rd.setBid(bbsname);
		rd.setParent(parent);

		manager.reply_insBoard(rd);
		
		if(bid==1){
			FileManager fmanager = new FileManager();
			
			BBSDto dto = new BBSDto();
			ArrayList<ReplyDto> redto = new ArrayList<ReplyDto>();
		   
			String tseq = request.getParameter("seq");
		    int seq = Integer.parseInt(tseq);
//		    int seq = 61;
		   	   ReplyDto rdto = new ReplyDto();
		   	   
		   	   rdto.setBid(bbsname);
		   	   rdto.setSeq(seq);
		   	   
		   	BBSDto tdto = new BBSDto();
		    tdto.setSeq(seq);
		    tdto.setBbsname(bbsname);
		    dto=bmanager.getBoard(tdto);
		    redto=manager.reGetBoards(rdto);
		    List<FileDto> filelists=fmanager.getFileList(seq);
			
		  
			
			request.setAttribute("board", dto);
			request.setAttribute("reboards", redto);
			request.setAttribute("fileList", filelists);
			forword.setUrl("view");
			forword.setRedirect(false);
		
			return forword;
		

			
		
		}else if(bid==2){
			FileManager fmanager = new FileManager();
			PhotoDto photo = null;
			ArrayList<ReplyDto> redto = new ArrayList<ReplyDto>();
			
			ReplyDto rdto = new ReplyDto();
					
			rdto.setBid(bbsname);
			rdto.setSeq(parent);
					
			photo = pmanager.getPhoto(parent);
			redto=manager.reGetBoards(rdto);
			
			List<FileDto> filelists=fmanager.getFileList(parent);
			
		
			
			request.setAttribute("reboards", redto);
			request.setAttribute("photo", photo);
			request.setAttribute("photolist", filelists);
			forword.setUrl("pview");
			forword.setRedirect(false);
			return forword;
		}
		

		return forword;
	}//
}
